#include "SList.h"

// 申请节点
SLTNode *ListByNode(DataType index)
{
    SLTNode *newnode = (SLTNode *)malloc(sizeof(SLTNode));
    if (newnode == NULL)
    {
        printf("%s\n", strerror(errno));
        exit(1);
    }
    newnode->data = index;
    newnode->next = NULL;
    return newnode;
}

// 头插
void SLTPushFront(SLTNode **pphead, DataType index)
{
    assert(pphead);
    // 申请节点
    SLTNode *newnode = ListByNode(index);
    newnode->next = *pphead;
    *pphead = newnode;
}

// 尾插
void SLTPushBack(SLTNode **pphead, DataType index)
{
    assert(pphead);
    // 申请节点
    SLTNode *newnode = ListByNode(index);
    if (*pphead == NULL)
    {
        *pphead = newnode;
    }
    else
    {
        SLTNode *ptail = *pphead;
        while (ptail->next)
        {
            ptail = ptail->next;
        }
        ptail->next = newnode;
    }
}

// 头删
void SLTPopFront(SLTNode **pphead)
{
    assert(pphead && *pphead);
    SLTNode *ptail = *pphead;
    *pphead = (*pphead)->next;
    free(ptail);
    ptail = NULL;
}

// 尾删
void SLTPopBack(SLTNode **pphead)
{
    assert(pphead && *pphead);
    if ((*pphead)->next == NULL)
    {
        free(*pphead);
        *pphead = NULL;
    }
    else
    {
        SLTNode *ptail = *pphead;
        while (ptail->next->next)
        {
            ptail = ptail->next;
        }
        free(ptail->next);
        ptail->next = NULL;
    }
}

// 指定位置之前插入
void SLTInsert(SLTNode **pphead, DataType index, SLTNode *pos)
{
    assert(pphead && *pphead && pos);
    // 申请节点
    SLTNode *newnode = ListByNode(index);

    if (*pphead == pos)
    {
        // 头插
        newnode->next = *pphead;
        *pphead = newnode;
    }
    else
    {
        SLTNode *ptail = *pphead;
        while (ptail->next != pos)
        {
            ptail = ptail->next;
        }
        // ptail newnode pos
        ptail->next = newnode;
        newnode->next = pos;
    }
}

// 指定位置删除
void SLTErase(SLTNode **pphead, SLTNode *pos)
{
    assert(pphead && *pphead && pos);
    if (*pphead == pos)
    {
        //头删
        SLTNode* next = (*pphead)->next;
        free(*pphead);
        *pphead = next;
    }
    else
    {
        SLTNode *ptail = *pphead;
        while(ptail->next != pos)
        {
            ptail = ptail->next;
        }
        SLTNode* next = pos->next;
        // ptail next
        ptail->next = next;
        free(pos);
        pos = NULL;

    }
}

// 打印链表
void SLTPrint(SLTNode **pphead)
{
    SLTNode *pcur = *pphead;
    while (pcur)
    {

        printf("%d->", pcur->data);
        pcur = pcur->next;
    }
    printf("NULL\n");
}

// 链表的查找
SLTNode *SLTFind(SLTNode **pphead, DataType index)
{
    assert(pphead);
    SLTNode *ptail = *pphead;
    while (ptail)
    {
        if (ptail->data == index)
        {
            return ptail;
        }
        ptail = ptail->next;
    }
    return NULL;
}

// 链表的销毁
void SLTDestory(SLTNode **pphead)
{
    assert(pphead);
    SLTNode* ptail = *pphead;
    while(ptail)
    {
        SLTNode* del = ptail;
        ptail = ptail->next;
        free(del);
        del = NULL;   
    }
    *pphead = NULL;
}
